package com.cloud.drore.eboss.operation.service.impl;

import com.cloud.drore.eboos.common.base.BaseServiceImpl;
import com.cloud.drore.eboos.common.base.Dictionary;
import com.cloud.drore.eboos.common.base.MyMapper;
import com.cloud.drore.eboos.common.base.ResultMessage;

import com.cloud.drore.eboos.common.entity.base.UserInfo;
import com.cloud.drore.eboos.common.util.LocalData;
import com.cloud.drore.eboos.exception.BusinessException;
import com.cloud.drore.eboss.operation.entity.Merchant;
import com.cloud.drore.eboss.operation.entity.POIOperation;
import com.cloud.drore.eboss.operation.entity.po.POIOperationPo;
import com.cloud.drore.eboss.operation.entity.po.POIPo;
import com.cloud.drore.eboss.operation.entity.vo.DictionaryPOIVo;
import com.cloud.drore.eboss.operation.mapper.MerchantMapper;
import com.cloud.drore.eboss.operation.mapper.POIMapper;
import com.cloud.drore.eboss.operation.service.IBaseDataFeign;
import com.cloud.drore.eboss.operation.service.IPOIService;
import com.cloud.drore.eboss.operation.supplierApiService.IPOISupplierApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/1/6 0006 下午 5:40 <br>
 * @Author: <a href="410508961@qq.com">夏凯</a>
 */
@Service
public class POIServiceImpl extends BaseServiceImpl implements IPOIService, IPOISupplierApiService {
    @Resource
    POIMapper POIMapper;

    @Autowired
    IBaseDataFeign iBaseDataFeign;

    @Resource
    MerchantMapper merchantMapper;

    private static String supplierType = "MERCHANT_SUPPLIER";

    @Override
    public MyMapper getMapper() {
        return POIMapper;
    }

    /**
     * @param
     * @ClassName: MerchantServiceImpl
     * @Return:
     * @Decription: 查询所有POI集合的接口
     * @CreateDate: Created in 2018/1/6 0006 下午 1:19
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ResultMessage findByName() {
        List<POIOperation> list = POIMapper.queryPoiList();
        return new ResultMessage(list);
    }

    /**
     * @param
     * @ClassName: POIServiceImpl
     * @Return: com.cloud.drore.eboos.common.base.ResultMessage
     * @Decription: 添加poi关联商户的时候选择列表
     * @CreateDate: Created in 2018/1/16 0016 下午 2:40
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ResultMessage findMerchantList() {

        return new ResultMessage(merchantMapper.queryMerchantList());
    }


    /**
     * @param POIOperation
     * @ClassName: POIServiceImpl
     * @Return: java.util.List<com.cloud.drore.eboss.operation.entity.POIOperation>
     * @Decription: 根据code（POI类型查询）列表,有code则查c包含code的，没有则查全部
     * @CreateDate: Created in 2018/1/19 0019 下午 6:00
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public List<POIOperation> selectPoiListByType(POIPo POIOperation) throws BusinessException {
        UserInfo currentUser = LocalData.getCurrentUser();
        //查询商户的类型
        if(currentUser.getTypeId()==-1){
            if (null == POIOperation.getPoiType() || "".equals(POIOperation.getPoiType())) {
                return POIMapper.queryPoiList();
            }
            return POIMapper.SelectPoiList(POIOperation.getPoiType());
        }
        Merchant merchant = merchantMapper.queryMerchantType(currentUser.getTypeId());
        if (merchant.getMerchantType().equals(supplierType)) {
            if (null == POIOperation.getPoiType() || "".equals(POIOperation.getPoiType())) {
                return POIMapper.queryPoiListByMerchantId(currentUser.getTypeId());
            }
            return POIMapper.SelectPoiListByMerchantId(POIOperation.getPoiType(), currentUser.getTypeId());
        }
        throw new BusinessException("当前用户信息异常！");
    }


    /**
     * @param
     * @ClassName: ConsumeServiceImpl
     * @Return: com.cloud.drore.eboos.common.base.ResultMessage
     * @Decription: POI页面初始化字段
     * @CreateDate: Created in 2018/1/15 0015 下午 2:24
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ResultMessage findInitDictionary() {

        DictionaryPOIVo dictionaryPOIVo = new DictionaryPOIVo();
        //查询字典得到POI类型
        dictionaryPOIVo.setPOIType(iBaseDataFeign.getDicListMapByCodeVo("POI_TYPE"));
        return new ResultMessage(dictionaryPOIVo);
    }

    /**
     * @param poiName
     * @ClassName: POIServiceImpl
     * @Return: java.lang.Boolean
     * @Decription: 根据查询poi查询得到的list数量判断有没有重名的
     * @CreateDate: Created in 2018/1/22 0022 下午 9:12
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public Boolean judgePoiName(String poiName) {
        List<POIOperation> operationList = POIMapper.findOperationList(poiName);
        if (operationList.size() >= 1) {
            return false;
        }
        return true;
    }

    /**
     * @param po
     * @ClassName: POIServiceImpl
     * @Return: void
     * @Decription: 保存poi的信息
     * @CreateDate: Created in 2018/4/8 0008 下午 3:09
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public void insertPoi(POIOperation po) {
        UserInfo currentUser = LocalData.getCurrentUser();
        po.setMerchantId(currentUser.getTypeId());
        Merchant merchant = merchantMapper.queryMerchantNameById(currentUser.getTypeId());
        po.setMerchantName(merchant.getMerchantName());
        insert(po);
    }

    /**
     * @param POIOperation
     * @ClassName: POIServiceImpl
     * @Return: int
     * @Decription: 修改poi的基本信息
     * @CreateDate: Created in 2018/1/26 0026 下午 4:05
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public int poiUpdate(POIOperationPo POIOperation) {
        return POIMapper.updatePOI(POIOperation);
    }
}
